﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Fireball.SyntaxParsers;
using Fireball.Syntax;
using Fireball.Syntax.Parser;
using System.IO;
using System.Windows.Markup;

namespace WpfTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            editor.FontFamily = new FontFamily("Courier New");
            editor.FontSize = 12;
            editor.Margin = new Thickness(10);

            var lang = (Language)XamlReader.Load(File.OpenRead("./CSharp.xaml"));

            Parser parser = new Parser(lang);

            SyntaxDocument doc = new SyntaxDocument();

            doc.Text = File.ReadAllText("d:\\test.txt");

            parser.Document = doc;

            parser.ParseAll();

            foreach (Fireball.Syntax.Line current in doc.Lines)
            {
                foreach (LineEntity c in current)
                {
                    if (c is Word)
                    {
                        Word w = (Word) c;

                        Run r = new Run(w.Text);
                        if (w.IsKeyword)
                        {
                            r.Foreground = new SolidColorBrush(Colors.Blue);
                            r.FontWeight = FontWeights.Bold;
                        }
                        editor.Inlines.Add(r);
                    }
                    else if (c is Space)
                    {
                        editor.Inlines.Add(" ");
                    }
                    else if (c is Operator)
                    {
                        Operator w = (Operator)c;

                        Run r = new Run(w.Text);

                        r.Foreground = new SolidColorBrush(Colors.Red);

                        editor.Inlines.Add(r);
                    }
                }
                editor.Inlines.Add(new LineBreak());
            }
        }
    }
}
